package com.xiaodou.mjf.entity.basic;


import com.google.common.collect.Lists;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

import com.xiaodou.mjf.entity.basic.base.RoleBase;

/***
 *
 * Role实体定义
 * @author XiaoDou.com Code Generator
 * @version 1.0
 * 2015-02-28 14:33:19
 *
 */
@Entity
@DynamicInsert @DynamicUpdate
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Role extends RoleBase implements Serializable{

	/**序列化标识*/
	private static final long serialVersionUID = 1948804771761345910L;

    @ManyToMany
    @JoinTable(name = "role_menu",joinColumns = {@JoinColumn(name="role_id")},inverseJoinColumns = {@JoinColumn(name = "menu_id")})
    @OrderBy("sort asc")
    private List<Menu> menus = Lists.newArrayList();

    @Transient
    private String menuIds;

    public Role(){}

    public Role(String id){
        this.id = id;
    }

    public List<Menu> getMenus() {
        return menus;
    }

    public void setMenus(List<Menu> menus) {
        this.menus = menus;
    }

    public String getMenuIds() {
        List<String> nameIdList = Lists.newArrayList();
        for (Menu menu : menus) {
            nameIdList.add(menu.getId());
        }
        return StringUtils.join(nameIdList, ",");
    }

    public void setMenuIds(String menuIds) {
        menus = Lists.newArrayList();
        if (menuIds != null){
            String[] ids = StringUtils.split(menuIds, ",");
            for (String menuId : ids) {
                Menu menu = new Menu(menuId);
                menus.add(menu);
            }
        }
    }
}
